﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ocio.Element;
using Microsoft.DirectX;
using TgcViewer.Utils.TgcSceneLoader;
using TgcViewer;
using Microsoft.DirectX.Direct3D;
using TgcViewer.Utils.TgcGeometry;

namespace AlumnoEjemplos.Ocio.Element
{
    public class Wall
    {
        List<Brick> ladrillos = new List<Brick>();
        Brick ladrilloTemplate;




        /**
         *Construye uan pared con las caracertieticas dadas con un Brick template predefinido.
         */
        public Wall(float ancho, float largo, float profundidad)
        {
            Device d3dDevice = GuiController.Instance.D3dDevice;
            TgcTexture texture = TgcTexture.createTexture(d3dDevice, GuiController.Instance.ExamplesMediaDir + "Texturas\\granito.jpg");
            Vector3 tamano = new Vector3(1,1, 1);
            this.ladrilloTemplate = Brick.fromSize(tamano, texture);
            this.procesarCaracteristicas(ancho, largo, profundidad, ladrilloTemplate);
        }


        public Wall(int ancho, int largo, int profundidad,Brick brickTemplate)
        {
            this.ladrilloTemplate = brickTemplate;
            this.procesarCaracteristicas(ancho, largo, profundidad, ladrilloTemplate);
          
        }

        private void procesarCaracteristicas(float ancho, float largo, float profundidad, Brick brickTeamplate)
        {
            Vector3 size=brickTeamplate.Size;
            float anchoLadrillo = size.X;
            float largoLadrillo = size.Y;
            float profundidadLadrillo = size.Z;

            int cantidadLadrillosAncho = (int)(ancho / anchoLadrillo) ;
            int cantidadLadrillosAlto = (int)(largo / largoLadrillo) ;
            int cantidadLadrillosProfunidad = (int)(profundidad / profundidadLadrillo) ;

            for (int k = 0; k < (cantidadLadrillosProfunidad); k++) { 
                for (int i = 0; i < cantidadLadrillosAncho; i++)
                {
                    Vector3 centroLadrilloX = new Vector3(this.ladrilloTemplate.Position.X + (this.ladrilloTemplate.Size.X * i), this.ladrilloTemplate.Position.Y, this.ladrilloTemplate.Position.Z+(this.ladrilloTemplate.Size.Z*-k));
                    Brick ladrilloX=Brick.fromSize(centroLadrilloX, this.ladrilloTemplate.Size, this.ladrilloTemplate.Texture);
                    ladrillos.Add(ladrilloX);
                    for (int j = 1; j < cantidadLadrillosAlto; j++)
                    {
                        Vector3 centroLadrilloY = new Vector3(ladrilloX.Position.X, ladrilloX.Position.Y + (this.ladrilloTemplate.Size.Y * j), ladrilloX.Position.Z);
                        Brick ladrilloY = Brick.fromSize(centroLadrilloY, this.ladrilloTemplate.Size, this.ladrilloTemplate.Texture);
                        ladrillos.Add(ladrilloY);
                    }
                }
            }

        }

     

        public void render()
        {
            foreach(Brick ladrillo in this.ladrillos){
                ladrillo.render();
            }
          
        }

    }
}
